import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;

/**
 * Creates the JFrame and its menu
 * 
 * @author  James Wilkinson <jhwilkin@purdue.edu>
 * @version 1.7
 * @since   2011-11-20
 **/
public class Frame extends JFrame implements MenuListener, ActionListener
{
  
    private GameCanvas myGameCanvas;
    private GameLogic myGameLogic;
    private HighScores myHighScores;
    
    private JMenuBar bar;
        
    private JMenu file;
    private JMenu options;
    
    private JMenuItem newGame;
    public JMenuItem pauseGame;
    private JMenuItem exitGame;
    private JMenuItem superLaser;
    private JMenuItem highScores;
        
    public JRadioButtonMenuItem easyDifficulty;
    public JRadioButtonMenuItem mediumDifficulty;
    public JRadioButtonMenuItem hardDifficulty;
    
    /**
     * @param inGameLogic  Used for pausing, unpausing and starting new games.  Also for adjusting difficulty
     * @param inGameCanvas Used to add the Canvas to the frame
     * @param inHighScores Used to display high scores
     **/
    public Frame(GameLogic inGameLogic, GameCanvas inGameCanvas, HighScores inHighScores)
    {
      
        //Set myGameCanvas, myGameLogic and myHighScores to equal
        //their corresponding passed parameters.
        myGameCanvas = inGameCanvas;
        myGameLogic = inGameLogic;
        myHighScores = inHighScores;
        
        //Setup the file menu.
        file = new JMenu("File");
         
        newGame = new JMenuItem("New Game");
        pauseGame = new JMenuItem("Pause Game");
        exitGame = new JMenuItem("Exit Game");
         
        newGame.addActionListener(this);
        pauseGame.addActionListener(this);
        exitGame.addActionListener(this);
         
        file.add(newGame);
        file.add(pauseGame);
        file.add(exitGame);
        
        //Setup the options menu
        options = new JMenu("Options");
        easyDifficulty = new JRadioButtonMenuItem("Easy");
        mediumDifficulty = new JRadioButtonMenuItem("Medium");
        hardDifficulty = new JRadioButtonMenuItem("Hard");
        superLaser = new JMenuItem("Super Laser is OFF");
        
        easyDifficulty.addActionListener(this);
        mediumDifficulty.addActionListener(this);
        hardDifficulty.addActionListener(this);
        superLaser.addActionListener(this);
         
        options.add(easyDifficulty);
        options.add(mediumDifficulty);
        options.add(hardDifficulty);
        options.addSeparator();
        options.add(superLaser);
         mediumDifficulty.setSelected(true);
        
        //Setup the high scores menu
        highScores = new JMenuItem("High Scores");
        highScores.addActionListener(this);
         
        //Setup the menu bar
        bar = new JMenuBar();
        bar.add(file);
        bar.add(options);
        bar.add(highScores);
         
        //Setup the frame using GridBagLayout
        setLayout(new GridBagLayout());
        setJMenuBar(bar);
         
        //Do nothing on click of X
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        
        //Create the application a 100px from the top/side of the screen
        setLocation(100,100);
        
        //Disable resizing of the window
        setResizable(false);
        
        //Add the game canvas
        add(myGameCanvas.c);
        
        //Shrink the frame to fit its contents
        pack();
        
        //Make the frame visible
        setVisible(true);
    }
    
    /**
     * Handles the selection of any menu item
     * 
     * @param e ActionEvent that occured
     **/
    public void actionPerformed(ActionEvent e)
    {
      
      Object s = e.getSource();
      //If new game selected, send a synchronous reset to the GameLogic
      
      if(s.equals(newGame)){
        myGameLogic.syncReset = true;
      }
        
      //If high scores selected, pause the game and show the high scores.
      if(s.equals(highScores)){
       
        myGameLogic.paused = true;
        myHighScores.showHighScores(myGameLogic.thisPerson, myGameLogic.thisScore);
        myGameLogic.paused = false;
        pauseGame.setText("Pause Game");
      }
      
      //If pause game selected, pause/unpause the game and set the menu item text to
      //"Pause Game" if unpaused or "Unpause Game" if paused.
      if(s.equals(pauseGame)){
   
        myGameLogic.paused = !myGameLogic.paused;
        
        if(myGameLogic.paused==true){
         pauseGame.setText("Unpause Game");
        }else{
         pauseGame.setText("Pause Game");
        }
      }
        
      //If exit game selected, save the high scores and then exit
      if(s.equals(exitGame)){
       
        myGameLogic.gameIsDone = true;
        dispose();
      }
            
      //If super laser selected, toggle the super laser and the menu item's display
      if(s.equals(superLaser)){
        
        Settings.superLaser = !Settings.superLaser;
        
        if(Settings.superLaser==true){
          superLaser.setText("Super Laser is ON");
        }else{
          superLaser.setText("Super Laser is OFF");
        }
      }
            
      //If easy difficulty selected, make sure its radio button is selected, turn off the other difficulties' radio buttons,
      //set the game difficulty and adjust the centipede delay accordingly.
      if(s.equals(easyDifficulty)){
        
        easyDifficulty.setSelected(true);
        mediumDifficulty.setSelected(false);
        hardDifficulty.setSelected(false);
        Settings.centDelay = Settings.centDelayEasy;
      }
      
      //If medium difficulty selected, make sure its radio button is selected, turn off the other difficulties' radio buttons,
      //set the game difficulty and adjust the centipede delay accordingly.
      if(s.equals(mediumDifficulty)){
        
        easyDifficulty.setSelected(false);
        mediumDifficulty.setSelected(true);
        hardDifficulty.setSelected(false);
        Settings.centDelay = Settings.centDelayMedium;
      }
      
      //If hard difficulty selected, make sure its radio button is selected, turn off the other difficulties' radio buttons,
      //set the game difficulty and adjust the centipede delay accordingly.
      if(s.equals(hardDifficulty)){
        
        easyDifficulty.setSelected(false);
        mediumDifficulty.setSelected(false);
        hardDifficulty.setSelected(true);
        Settings.centDelay = Settings.centDelayHard;
      }
    }
    
    //Unused
    public void menuCanceled(MenuEvent e){}
    public void menuDeselected(MenuEvent e){}
    public void menuSelected(MenuEvent e){}
}